﻿// (c) 版权所有 TriangleGames 2012-2012

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using TriangleGames.TrainResEditor;

namespace TriangleGames.TrainResEditor
{
    [CustomEditor(typeof(TrainResGlobalsCore))]
    public class TrainResGlobalsInspectorCore : Editor
    {
        private TrainResGlobalsCore globals;
        private bool refresh;

        private List<FsmVariable> variableList;

        private GUIStyle warningBox;

        void OnEnable()
        {
            //Debug.Log("TrainResGlobalsInspector: OnEnable");

            globals = target as TrainResGlobalsCore;

            BuildVariableList();
        }

        public override void OnInspectorGUI()
        {
            if (warningBox == null)
            {
                warningBox = new GUIStyle(EditorStyles.boldLabel);
                warningBox.wordWrap = true;
            }

            GUILayout.Label("注意: 观察器中不会被重置当停止运行时! 重新选择TrainResGlobals并点击Play/Stop进行重置.", warningBox);

            if (refresh)
            {
                Refresh();
                return;
            }

            GUILayout.Label("全局变量", EditorStyles.boldLabel);

            if (variableList.Count > 0)
            {
                foreach (var fsmVariable in variableList)
                {
                    var tooltip = fsmVariable.Name;

                    if (!string.IsNullOrEmpty(fsmVariable.Tooltip))
                    {
                        tooltip += "\n" + fsmVariable.Tooltip;
                    }

                    fsmVariable.DoValueGUI(new GUIContent(fsmVariable.Name, tooltip), true);
                }
            }
            else
            {
                GUILayout.Label("[空]");
            }

            GUILayout.Label("全局事件", EditorStyles.boldLabel);

            if (globals.Events.Count > 0)
            {
                foreach (var eventName in globals.Events)
                {
                    GUILayout.Label(eventName);
                }
            }
            else
            {
                GUILayout.Label("[空]");
            }

            /*		GUILayout.Space(5);

                    if (GUILayout.Button(new GUIContent("刷新列表", "Edit Globals in the TrainRes FSM Editor.")) || globals.NeedsRefresh) // TODO
                    {
                        refresh = true;
                        GUIUtility.ExitGUI();
                    }*/
        }

        void Refresh()
        {
            refresh = false;
            BuildVariableList();
            Repaint();
        }

        void BuildVariableList()
        {
            variableList = FsmVariable.GetFsmVariableList(globals.Variables, globals);
        }
    }
}